package com.df.bpm.utils.businessViewerV2.business;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.res.V_scrollPanel;
import com.df.common.utils.CommonUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class V_scroll4BusinessSteps extends V_scrollPanel<M_businessLocationsDTO4Subset> {


    V_businessByOutput baba;

    public V_scroll4BusinessSteps(V_businessByOutput baba){
        this.baba=baba;
        List<M_businessLocationsDTO4Subset> mBusinessLocationsDTO4Subsets=C_business.getLocationDto4Subsets(baba.activeBusiness,baba.activeGroup);
        this.putModel(mBusinessLocationsDTO4Subsets);
        String iTitle="业务流程";
        this.setBorder(BorderFactory.createTitledBorder(iTitle));
    }

    @Override
    public List<String> getColNames() {
        return Arrays.asList("位置ID","步骤","担当者","业务描述","子集","创建时间","更新时间");
    }

    @Override
    public void hideCol(JTable iTable) {
        Main.hideCol(iTable,0);
    }

    @Override
    public List<Integer> colWidthSetting() {
        return new ArrayList<>(Arrays.asList(1,3,5,21,4,11,11));
    }

    @Override
    public Object[] getRowValues(M_businessLocationsDTO4Subset row) {
        return new Object[]{row.getBusinessLocationsIndex(),row.getSequence(),
                row.getEntityName(),row.getBusinessDesc(),row.getSubset(),
                dateFormat.format(row.getCreateDate()),
                dateFormat.format(row.getUpdateDate())};
    }

    @Override
    public M_businessLocationsDTO4Subset getEmptyRow() {
        return new M_businessLocationsDTO4Subset();
    }

    @Override
    public MouseListener mouseAdapter4Table(JTable iTable, List<M_businessLocationsDTO4Subset> list) {

        iTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//为了拖拽调序，改成单选
        return new MouseAdapter() {
            int oRow=-1;
            int nRow=-1;
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = iTable.rowAtPoint(e.getPoint());
                int col = iTable.columnAtPoint(e.getPoint());
                if (row >= 0 ) {
                    if (e.getClickCount() == 2) {// 处理双击事件
                        if(col==1){// 查询业务详情
                            M_businessLocationsDTO4Subset temp=list.get(row);
                            baba.setActiveBusiness(temp.getBusinessInformationIndex(),temp.getBusinessDesc());
                            baba.vHistory.insert(temp.getBusinessInformationIndex(),temp.getBusinessDesc());
                        }else if((col==2)||(col==3)){//更换步骤对应的业务
                            if(baba.activeBusiness!=null){
                                M_businessDto4Search nStep=C_business.selectOne(baba.goldenRequirement);
                                if(nStep!=null){
                                    M_businessLocationsDTO4Subset temp = list.get(row);
                                    if(nStep.getBusinessInformationIndex()==-1){//删除
                                        if(temp.getBusinessLocationsIndex()!=null){
                                            C_business.deleteBusinessLocation(temp.getBusinessLocationsIndex());
                                        }
                                    }else{//增、改
                                        if (!Objects.equals(temp.getBusinessInformationIndex(), nStep.getBusinessInformationIndex())){
                                            M_businessLocationsDTO4Subset nLocation = getmBusinessLocationsDTO4Subset(temp, nStep, row);
                                            C_business.updateBusinessLocation(nLocation);
                                        }
                                    }
                                    baba.setActiveGroup(baba.activeGroup);
                                }
                            }

                        }
                    }else if (e.getClickCount() == 1){
                        M_businessLocationsDTO4Subset temp=list.get(row);
                        if(temp!=null && temp.getBusinessInformationIndex()!=null){
                            baba.showBusinessInputOutput(temp.getBusinessInformationIndex());
                        }
                    }
                }
            }

            public void mousePressed(MouseEvent event)
            {
                try {
                    int iRow=iTable.getSelectedRow();
                    if(CommonUtils.varIsBlank(list.get(iRow).getBusinessLocationsIndex())){
                        oRow=-1;
                    }else{
                        oRow=iRow;
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null,e.getMessage(), "注意",JOptionPane.INFORMATION_MESSAGE);
                }
            }
            public void mouseReleased(MouseEvent event)
            {
                try {
                    int iRow=iTable.getSelectedRow();
                    if(iRow>=0 && oRow>=0) {
                        if(CommonUtils.varIsBlank(list.get(iRow).getBusinessLocationsIndex())){
                            nRow=iRow-1;
                        }else{
                            nRow=iRow;
                        }
                        if(nRow!=oRow && nRow>=0) {
                            Integer nSeq=list.get(nRow).getSequence();
                            list.get(oRow).setSequence(nSeq);
                            C_business.updateBusinessLocation(list.get(oRow));
                            baba.setActiveGroup(baba.activeGroup);
                        }
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null,e.getMessage(), "注意",JOptionPane.INFORMATION_MESSAGE);
                }finally {
                    oRow=-1;
                    nRow=-1;
                }
            }
        };
    }

    @NotNull
    private M_businessLocationsDTO4Subset getmBusinessLocationsDTO4Subset(M_businessLocationsDTO4Subset temp, M_businessDto4Search nStep, int row) {
        M_businessLocationsDTO4Subset nLocation=new M_businessLocationsDTO4Subset();
        nLocation.setBusinessLocationsIndex(temp.getBusinessLocationsIndex());
        nLocation.setBusinessInformationIndex(nStep.getBusinessInformationIndex());
        nLocation.setSuperior(baba.activeBusiness);
        nLocation.setGroupID(baba.activeGroup);
        nLocation.setSequence(row +1);
        return nLocation;
    }

}
